/*
	The MIT License

	Copyright (c) 2008 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#pragma once


struct	EMShader_s {
		CGprogram	program;
		uint64_t	def_bits;
	};


class	EMShaderSource {
	public:
					EMShaderSource	( const char *path, class EMShaderSystem *shader_sys );
					~EMShaderSource	( void );
					
		const char	*Path			( void );
					
		void		SetDefine		( uint64_t bit, const char *def );
		void		SetPixelShader	( uint64_t bits, bool compile_only = false );
		void		SetVertexShader	( uint64_t bits, bool compile_only = false );
		void		SetGeomShader	( uint64_t bits, bool compile_only = false );
		
		void		CleanShaders	( void );
		
		void		SetPSConst		( const char *name, const EVec4 &v );
	
	protected:
		void		CheckError		( void );
		EMShader_s	*CompileShader	( CGprofile prof, const char *entry, uint64_t bits, vector<string> &add_defs );
	
		class EMShaderSystem	*shader_sys;
		string source_path;
		string definitions[64];
		CGprofile	vs_prof;
		CGprofile	ps_prof;
		CGprofile	gs_prof;
		CGcontext	context;
		
		EMShader_s *current_ps;
		EMShader_s *current_vs;
		EMShader_s *current_gs;
		
		vector<EMShader_s*>	vs_list;
		vector<EMShader_s*>	gs_list;
		vector<EMShader_s*>	ps_list;
	};


class	EMShaderSystem {
	public:
						EMShaderSystem	( void );
						~EMShaderSystem	( void );
						
		EMShaderSource	*RegisterShader	( const char *path );
		
		CGprofile		VertexProfile	( void ) { return vs_prof; }
		CGprofile		GeomProfile		( void ) { return gs_prof; }
		CGprofile		PixelProfile	( void ) { return ps_prof; }
		CGcontext		Context			( void ) { return context; }
		
		void			DisableShaders	( void );
		
		static void		ReloadShaders_f	( EMShaderSystem *self, int argc, char **argv );
		
	protected:
		CGprofile	vs_prof;
		CGprofile	ps_prof;
		CGprofile	gs_prof;
		CGcontext	context;
		vector<EMShaderSource*>	shader_sources;
	};